package org.inote.controller;

import com.github.pagehelper.PageInfo;
import org.inote.common.KeyStatus;
import org.inote.pojo.tablePojo.Note;
import org.inote.pojo.tablePojo.User;
import org.inote.pojo.voPojo.NoteVo;
import org.inote.service.NoteService;
import org.inote.service.UserService;
import org.inote.utils.FileUploadUtil;
import org.inote.utils.MarkdownUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/note")
public class NoteController {

    @Resource
    private NoteService noteService;
    @Resource
    private UserService userService;
    /**
     * 用户添加笔记，若添加成功，将跳转至用户主页，并能看到最新添加的笔记
     * 用户笔记数加一
     * @return
     */
    @RequestMapping("/addNote")
    public String addNote(Note note, HttpSession session){
        User user = (User) session.getAttribute(KeyStatus.USER_INFO);
        //用户信息失效，重新登录
        if(user == null){
            return "redirect:/login.jsp";
        }

        note.setUserId(user.getUserId());
        note.setNoteDate(new Date());
        System.out.println(note);
        noteService.addNote(note,user.getUserId());
        User user_a = userService.queryUserInfoByUserId(user.getUserId());
        session.setAttribute(KeyStatus.USER_INFO,user_a);
        //跳转用户主页
        return "redirect:/enterMyHome";
    }

    /**
     * 根据noteId唯一删除一篇笔记
     * @param noteId
     * @return
     */
    @RequestMapping("/deleteNote")
    public String deleteNote(Integer noteId,HttpSession session){
        User user = (User) session.getAttribute(KeyStatus.USER_INFO);
        noteService.deleteNote(noteId,user.getUserId());
        User user_a = userService.queryUserInfoByUserId(user.getUserId());
        session.setAttribute(KeyStatus.USER_INFO,user_a);
        return "redirect:/enterMyHome";
    }

    @RequestMapping("/getNote")
    public String blog(Integer noteId,Model model)
    {
        model.addAttribute("notemk",noteService.getAndConvert(noteId));
        return "noteinfo";
    }

    @RequestMapping("/getNotesByPage")
    @ResponseBody
    public PageInfo<NoteVo> getNotesByPage(Model model,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,String condition)
    {
        PageInfo<NoteVo> info = noteService.queryByPage(pageNum,condition);

        return info;
    }

    @RequestMapping("/getCommentNoteList")
    @ResponseBody
    public List<Note> getCommentNoteList()
    {
        List<Note> notes =  noteService.queryNoteByContent();
        return notes;
    }



}
